package com.atguigu.order.service.impl;

import com.atguigu.commonUtils.orderVo.CourseWebVoOrder;
import com.atguigu.commonUtils.orderVo.MemberOrder;
import com.atguigu.order.entity.Order;
import com.atguigu.order.feign.EduClient;
import com.atguigu.order.feign.UcenterClient;
import com.atguigu.order.mapper.OrderMapper;
import com.atguigu.order.service.OrderService;
import com.atguigu.order.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 生成订单，返回订单号
     *
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public String createOrderByCourseId(String courseId, String memberId) {
        //通过远程调用 根据用户id 获取到 用户信息
        MemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        //通过远程调用 根据课程id 获取到 课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建order对象，向order对象里面设置 需要的数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //订单号,订单号唯一
        order.setCourseId(courseId);//课程id
        order.setCourseCover(courseInfoOrder.getCover());//课程封面
        order.setCourseTitle(courseInfoOrder.getTitle());//课程标题
        order.setTeacherName(courseInfoOrder.getTeacherName());//课程讲师

        order.setMemberId(memberId);//用户id
        order.setMobile(userInfoOrder.getMobile());//用户手机号
        order.setNickname(userInfoOrder.getNickname());//用户昵称

        order.setTotalFee(courseInfoOrder.getPrice());//课程价格
        order.setStatus(0);//支付状态 未支付 0 已支付 1
        order.setPayType(1); //支付类型 微信 1

        baseMapper.insert(order);

        //返回订单号
        return order.getOrderNo();
    }
}
